﻿using System.Collections.Generic;
using System.Text;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;

namespace SdmxMl.SanteSuisse
{
    public class QuestHtml
    {
        private ConceptSchemeType _cst;
        private ProcessType _process;

        public QuestHtml(ConceptSchemeType c, ProcessType p)
        {
            _cst = c;
            _process = p;
        }


        private List<ConceptType> GetChildConcepts(ConceptType ct)
        {
            List<ConceptType> list = new List<ConceptType>();

            foreach (ISdmxHierarchyListItem item in _cst.ConceptList)
            {
                if (item.Parent ==ct.Id)
                    list.Add(item as ConceptType);
            }

            return list;
        }

        private string GenerateQuestionPage(ProcessStepType pst, string lg, ConceptType ct)
        {
            StringBuilder sb = new StringBuilder(1024);
            sb.Append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");

            sb.Append("<head><title>").Append("Q").Append(pst.Id).Append("</title></head>");

            sb.Append("<body><table width='600px' border='1' frame='border' align='center' style='font-family: Arial, Helvetica, sans-serif'>");

            // Question
            sb.Append("<tr><td class='style1' align='center' style='width: 100px'>");
            sb.Append("<img alt='' src='Images/Question.jpg' style='width: 48px; height: 48px' /><br /><b>");
            sb.Append(pst.Id).Append("</b></td><td>");
            sb.Append(pst.Description.GetForLanguage(lg)).Append("</td></tr>");

            // INF
            sb.Append("<tr><td class='style1' align='center'>");
            sb.Append("<img alt='' src='Images/micro.jpg' style='width: 48px; height: 48px' />");
            sb.Append("</td><td>");
            sb.Append(pst.Computation.GetForLanguage(lg, null)).Append("</td></tr>");

            // Answer area
            sb.Append("<tr><td class='style1' align='center'>");
            sb.Append("<img alt='' src='Images/checklist.jpg' style='width: 96px; height: 89px' />");
            sb.Append("</td><td>");
            if (ct != null)
            {
                if (ct.CoreRepresentation.IsFilled)
                {
                    CodeListType clt = ReferenceManager.LocateCodelist(ct.CoreRepresentation);
                    if (clt != null)
                    {
                        string radioId = "RadioButtonList1";
                        sb.Append("<table id='").Append(radioId).Append("' border='0'>");
                        int index = 0;
                        foreach (CodeType c in clt.CodeList)
                        {
                            string curRadioId = radioId + "-" + index.ToString();

                            sb.Append("<tr><td><input id='").Append(curRadioId).Append("'");
                            sb.Append(" type='radio' name='").Append(radioId).Append("' value='");
                            sb.Append(c.Id);
                            sb.Append("' /><label for='").Append(curRadioId).Append("'>");
                            sb.Append(c.Id).Append(" - ").Append(c.Name.GetForLanguage(lg));
                            sb.Append("</label></td></tr>");
                            ++index;
                        }

                        AnnotationList anl = ct.AnnotationList.GetAllAnnotationOfType("CODE_QUALITY", null);
                        foreach (Annotation a in anl)
                        {
                            string curRadioId = radioId + "-" + index.ToString();

                            sb.Append("<tr><td><input id='").Append(curRadioId).Append("'");
                            sb.Append(" type='radio' name='").Append(radioId).Append("' value='");
                            sb.Append(a.Title);
                            sb.Append("' /><label for='").Append(curRadioId).Append("'>");
                            sb.Append(a.Title).Append(" - ").Append(a.Text.GetForLanguage(lg));
                            sb.Append("</label></td></tr>");
                            ++index;
                        }
                        sb.Append("</table></td></tr>");
                    }
                }
                else
                {
                    List<ConceptType> lc = GetChildConcepts(ct);
                    if (lc.Count > 0)
                    {
                        string checkId = "CheckBoxList1";
                        sb.Append("<table id='").Append(checkId).Append("' border='0'>");
                        int index = 0;
                        foreach (ConceptType c in lc)
                        {
                            sb.Append("<tr><td><input id='").Append(checkId).Append('_').Append(index).Append("'");
                            sb.Append("  type='checkbox' name=").Append(checkId).Append('$').Append(index).Append("' />");
                            sb.Append("<label for='").Append(checkId).Append('_').Append(index).Append("'>");
                            MultiLang ml = c.AnnotationList.GetInternationalizedTextOfAnnotationType("QUESTION");
                            string quest = ml == null ? "?" : ml.GetForLanguage(lg);
                            sb.Append(quest).Append("</label></td></tr>");
                        }
                        AnnotationList anl = ct.AnnotationList.GetAllAnnotationOfType("CODE_QUALITY", null);
                        foreach (Annotation a in anl)
                        {
                            sb.Append("<tr><td><input id='").Append(checkId).Append('_').Append(index).Append("'");
                            sb.Append("  type='checkbox' name=").Append(checkId).Append('$').Append(index).Append("' />");
                            sb.Append("<label for='").Append(checkId).Append('_').Append(index).Append("'>");
                            sb.Append(a.Title).Append(") ").Append(a.Text.GetForLanguage(lg)).Append("</label></td></tr>");
                            ++index;
                        }
                        sb.Append("</table></td></tr>");
                    }
                    else
                        sb.Append("&nbsp;").Append("</td></tr>");

                }
            }


            //Navigation
            if (pst.TransitionList.Count > 0)
            {
                sb.Append("<tr><td class='style1' align='center'>");
                //  sb.Append("<LINK rel='").Append(pst.TransitionList[0].TargetStep).Append("'");
                //  sb.Append(" href = '").Append("Q").Append(pst.TransitionList[0].TargetStep).Append(".htm'>");
                sb.Append("<a href='").Append("Q").Append(pst.TransitionList[0].TargetStep).Append(".htm'");
                sb.Append("<img src='Images/useLink.jpg' style='width: 48px; height: 48px' border='0'> </a>");
                sb.Append("</td><td>");
                // All conditional transitions
                for (int i = 1; i < pst.TransitionList.Count; ++i)
                {
                    sb.Append("<A href='");
                    sb.Append("Q").Append(pst.TransitionList[i].TargetStep).Append(".htm'>");
                    sb.Append(pst.TransitionList[i].Condition).Append("</A><br>");
                }


                sb.Append("</td></tr>");
            }

            // Final
            sb.Append("</table></body></html>");

            return sb.ToString();
        }
        
    }
}
